// pages/member/index/index.js
var app = getApp();
var core = app.requirejs('core');
var diypage = app.requirejs('biz/diypage');
var diyad = app.requirejs('biz/ad');
var $ = app.requirejs('jquery');
const util = require('../../../utils/util.js');
const api = require("../../../utils/api.js");
const authScope = require("../../../utils/auth.js")
Page(app.sr.page({

  /**
   * 页面的初始数据
   */
  data: {
    diypages: {},
    orderlist: [{
      adImg: '/static/images/member/zf.png',
      adText: '待支付',
      adLink: '/pages/order/index?status=0'
    }, {
      adImg: '/static/images/member/fh.png',
      adText: '待发货',
      adLink: '/pages/order/index?status=1'
    }, {
      adImg: '/static/images/member/sh.png',
      adText: '待收货',
      adLink: '/pages/order/index?status=2'
    }, {
      adImg: '/static/images/member/shouhuo.png',
      adText: '已收货',
      adLink: '/pages/order/index?status=3'
    }],
    fuwulist: [{
      adImg: '/static/images/member/map.png',
      adText: '地址管理',
      adLink: 'selectAddress'
    }, {
      adImg: '/static/images/member/hf.png',
      adText: '在线客服',
      adLink: 'service'
    }, {
      adImg: '/static/images/member/ys.png',
      adText: '隐私政策',
      adLink: '/pages/custom/index?pageid=102260 '
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar({
      // animation: true,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let data = {};
    let member = app.getCache('userInfo')
    let mobile = app.getCache('mobile')
    mobile = mobile != "" && mobile != undefined && mobile != "0";
    let $this = this
    let isLogin = app.checkAuth();
    data.isMobile = mobile;
    data.isLogin = isLogin;
    if (!isLogin) {
      data.avatarUrl = "../../../static/images/avatar.jpg";
      data.nickName = "Hi,游客请先登录";
    } else {
      var userInfo = app.getCache('userInfo');
      if (app.getCache('userInfo')) {
        data.avatarUrl = userInfo.avatar;
        data.nickName = userInfo.nickname;
        data.canGet = false;
      }
    }
    app.miniprogram(function (res) {
      //判断是否显示客服悬窗
      let globalData = app.globalData
      $this.setData({
        globalData: globalData,
        member: member
      })
    })
    this.setData(data);
    $this.initFunction();
    $this.getDiffStatusCount()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh({
      complete: (res) => {},
      fail: (res) => {},
      success: (res) => {},
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  initFunction: function () {
    var $this = this;
    // 店铺装修 会员中心
    diypage.get(this, 'member', {}, function (res) {
      // $this.getDiypage(res)
      // var pagetitle = sysset.shopname || '个人中心';
      if ($this.data.diypages.basicOption) {
        let page = $this.data.diypages
        wx.setNavigationBarTitle({
          title: page.basicOption.pageTitle
        });
        wx.setNavigationBarColor({
          frontColor: page.basicOption.tabColor == 'white' ? '#ffffff' : '#000000',
          backgroundColor: page.basicOption.tabBackColor,
        })
      }
    });
    /**启动广告 */
    diyad.get(this, function (res) {


    })
  },
  // 获取订单角标数字
  getDiffStatusCount() {
    let $this = this
    let apicontent = api.orders.getDiffStatusCount()
    core.post(apicontent, {}, function (res) {
      let StatusCount = res.data
      $this.setData({
        StatusCount
      })
    })
  },
  // 广告关闭 
  closeAdver() {
    this.setData({
      showAdver: false
    })
  },

  // 路由跳转
  navigateMf(e) {
    var url = e.currentTarget.dataset.url
    var re = /^[0-9]+.?[0-9]*/; //判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/ 

    //跳转其他小程序
    if (url.appid) {
      wx.navigateToMiniProgram({
        appId: url.appid,
        path: url.urldata,
        extraData: {
          // foo: 'bar'
        }, //跳转到其他小程序传递的参数
        envVersion: 'develop',
        success(res) {
          wx.showModal({
            title: '跳转其他小程序',
            content: '',
            showCancel: false

          })
        },
        fail(res) {
          wx.showModal({
            title: '跳转失败',
            content: res.errMsg,
            showCancel: false
          })
        }
      })
    } else if (url.includes('?cate')) {
      let params = url.split('?')
      if (params[1].includes('cate')) {
        let cateid = params[1].split('=')[1]
        app.setCache("cateid", cateid)
      }
      wx.switchTab({
        url: url,
      })
    } else if (re.test(url)) {
      wx.makePhoneCall({
        phoneNumber: url //仅为示例，并非真实的电话号码
      })
    } else if (url == '/pages/index/index') {
      wx.switchTab({
        url: url,
      })
    } else {
      wx.navigateTo({
        url: url,
        fail: function () {
          wx.switchTab({
            url: url,
          })
        }
      })
    }
  },
  //电话客服
  TelephoneService() {
    wx.makePhoneCall({
      phoneNumber: '4008206362' //仅为示例，并非真实的电话号码
    })
  },
  //获取登录授权
  bindgetuserinfo() {
    let $this = this;
    authScope.scopeLogin().then((res) => {
      if (res) {
        $this.onShow();
      }
    })
  },
  /**
   * 原生添加地址
   */
  selectAddress() {
    let $this = this
    wx.chooseAddress({
      success: function (res2) {
        let addressDetail = {
          userName: res2.userName,
          telNumber: res2.telNumber,
          cityName: res2.cityName,
          countyName: res2.countyName,
          detailInfo: res2.detailInfo,
          nationalCode: res2.nationalCode,
          postalCode: res2.postalCode,
          provinceName: res2.provinceName,
          full_region: res2.provinceName + res2.countyName + res2.detailInfo
        }
        app.setCache('address', addressDetail);
      },
      fail: (error) => {
        $this.checkAuthorize('scope.address')
      }
    })
  },
  //验证用户是否授权地址
  checkAuthorize(scope) {
    wx.getSetting({
      success: (res) => {
        // console.log(res.authSetting[scope])
        if (!res.authSetting[scope]) {
          wx.showModal({
            title: '用户未授权',
            content: '拒绝授权将不能体验小程序完整功能，点击确定开启授权',
            success: (res) => {
              // console.log(res)
              if (res.confirm) {
                wx.openSetting({})
              }
            }
          })
        }
      }
    })
  },
  //打开客服弹窗
  opencontact() {
    this.setData({
      contactbox: true
    })
  },
  //关闭
  upcontact() {
    this.setData({
      contactbox: false
    })
  },
  //客服
  handleContact() {

  }
}))